/*
 * @Author: Fung Junjie
 * @Date: 2023-10-27 14:25:30
 * @LastEditors: Fung Junjie
 * @LastEditTime: 2023-12-27 10:42:19
 * @FilePath: \client\src\views\AnalyseData\AntCurrenty\static\js\dataSource.js
 * @Description: 蚂蚁货币全局遍历数据
 */
// vscode命令cdsjs自动生成./static/js/dataSource.js
import isArray from 'lodash/isArray';
import merge from 'lodash/merge';
import { ALL_STATIC_FIELD } from './const';
import { getOptionDynamicList } from '@/api';
import userFieldMap from './user/index';
import questionFieldMap from './question/index';
import marketFieldMap from './market/index';
import meetFieldMap from './meet/index';
import labelFieldMap from './label/index';
import matchFieldMap from './match/index';
export const dataSource = {};
merge(
  dataSource, 
  userFieldMap,
  questionFieldMap,
  marketFieldMap,
  meetFieldMap,
  labelFieldMap,
  matchFieldMap,
);
export function getDataSource(moduleName, fieldName) {
  return dataSource[moduleName][fieldName];
}
export function getTableLabel(optionKey, value, option = {}) {
  const fieldMap = ALL_STATIC_FIELD;
  const item = fieldMap[optionKey]?.find((item) => item[option.value || 'value'] === value) || {};
  return item[option.label || 'label'];
}
// optionDynamic 请求参数
// optionValue 下拉对象的值
// optionLabel 下拉对象的文字
// optionLabelText 下拉展示的文字，如果是数组则以空格拼起来
// optionMode 下拉接口返回默认是标准的，但是有一些接口是老的，
// 接口返回是一个原始数组并不是一个标准的json数据结构
// 比如 ['value1','value2','value3'，'value4']
export function getOptionList(optionProp, { optionDynamic, optionKey, optionValue, optionLabel, optionLabelText, optionMode, optionChildName }) {
  // eslint-disable-next-line no-async-promise-executor
  return new Promise(async (resolve) => {
    if (optionDynamic) {
      const res = await getOptionDynamicList(optionDynamic);
      if (optionMode === 'tree') {
        const dynamicList = handleTreeStructure(res.data || res.rows, { optionValue, optionLabel, optionChildName });
        resolve(dynamicList);
      }
      const dynamicList = (res.data || res.rows).map((item) => {
        if (optionMode === 'old') {
          return {
            [optionValue]: item,
            [optionLabel]: item,
          };
        } else {
          let label = '';
          if (isArray(optionLabelText)) {
            optionLabelText.map((key) => {
              label += item[key] + ' ';
            });
          } else {
            label = optionLabelText;
          }
          label && label.indexOf('.') !== -1 && (label = label.slice(0, label.indexOf('.')));
          return {
            [optionValue || 'value']: item[optionValue],
            [optionLabel || 'label']: isArray(optionLabelText) ? label : item[optionLabel],
          };
        }
      });
      resolve(dynamicList);
    } else {
      const dataMap = ALL_STATIC_FIELD;
      resolve(dataMap[optionKey]);
    }
  });
}
// 获取单个静态下拉
export function getSimpleOption(optionKey) {
  return ALL_STATIC_FIELD[optionKey];
}

// 处理接口的树形结构
export function handleTreeStructure(res = [], { optionValue, optionLabel, optionChildName }) {
  const treeOption = res.map((item) => {
    const optionItem = {};
    if (item[optionChildName] && item[optionChildName].length) {
      optionItem[optionChildName] = handleTreeStructure(item[optionChildName] || [], { optionValue, optionLabel, optionChildName });
    }
    optionItem[optionValue] = item[optionValue];
    optionItem[optionLabel] = item[optionLabel];
    return optionItem;
  });
  return treeOption;
}
